<?php
include_once '../../lib/config.php';
include_once '../../lib/db.php';
include '../../lib/utils/date.php';
include '../../lib/utils/tag.php';
include '../../model/karyawan.php';
include '../../model/perbuatan.php';
include '../../model/detil_prestasipelanggaran.php';
include '../../model/prestasipelanggaran.php';
include '../../model/report.php';
include '../../model/report2.php';

$proc = $_REQUEST['proc'];

switch ($proc):
case 'graph':
	include ("../../lib/jpgraph/jpgraph.php");
	include ("../../lib/jpgraph/jpgraph_bar.php");
	
	//generate data
	$startPeriode = $_REQUEST['periode1'];
	$endPeriode = $_REQUEST['periode2'];
	$detachievID = $_REQUEST['detachievID'];
	if (!$detachievID || !$startPeriode || !$endPeriode) { return; }
	
	$detachievID = is_array($detachievID)? $detachievID: array($detachievID);
	
	$graphData = array();
	foreach($detachievID as $dd){
		$ddd = mysql_fetch_assoc(deprepel_load($dd));
		$graphData ['label'][] = $ddd['DETACHIEV_NAMA'];
	}
	
	$CSIM = array();
	$tmp_data = rep_totalPelanggaranDepartemen($detachievID, date('Y-m-d', $startPeriode), date('Y-m-d', $endPeriode) );
	foreach ( $detachievID  as $dd){
		$graphData['data'][] = $tmp_data[$dd]? $tmp_data[$dd] : 0;
		$CSIM['target'][] = "load_drillGraph(".json_encode(array(
				'detachievID'=> $dd,
				'startPeriode'=> $startPeriode,
				'endPeriode'=> $endPeriode,
			)).")";
		$CSIM['alt'][] = "%0.2f";
	}
	
	//normalisasi data jika datanya kosong
	if(count($graphData['data'])<=0){
		foreach($detachievID as $dd){
			$graphData['data'][] = 0;	
		}
	}
	
	//print_r($graphData);

	//Create the bar plot	
	$bplot1 = new BarPlot($graphData['data']);
	$bplot1-> SetFillColor("blue");
	$bplot1->SetWidth(0.5);
	$bplot1->SetLegend('Jumlah Pelanggaran');
	$bplot1->SetCSIMTargets($CSIM['target'],$CSIM['alt']);
	$bplot1->value->SetFormat('%0.2f');
	$bplot1->value->Show();
	
	// Create the graph.
	$graph = new Graph(700,500,"auto");
	$graph->SetFrame(false,'white@.5',1); 
	$graph->title->Set("Total Pelanggaran Seluruh Departemen");
	$graph->SetColor("white"); 
	$graph->SetScale("textlin");
	$graph->img->SetMargin(80,30,40,80);
	$graph->img->SetAntiAliasing(); 
	
	$graph->ygrid->SetFill(true,'#FFFFFF@0.5','#d7e5f5@0.5');
	$graph->yaxis->SetFont(FF_FONT1,FS_BOLD);
	$graph->yaxis->SetPos("min");
	$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
	$graph->yaxis->title->set("Jumlah Pelanggaran");
	$graph->yaxis->SetTitleMargin(50); 
	
	$graph->xaxis->SetFont(FF_FONT1,FS_BOLD);
	$graph->xaxis->SetPos("min");
	$graph->xaxis->SetTickLabels($graphData['label']);
	$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
	$graph->xaxis->title->set("Kategori Pelanggaran");
	if ( count($graphData['label'])>10 ) {
		$graph ->xaxis->SetTextLabelInterval(count($graphData['label'])/4);
	}
	
	$graph->legend->SetLayout(LEGEND_HOR);
	$graph->legend->Pos(0.4,0.95,"center","bottom");
	
	// Add the plot to the graph
	$graph->Add($bplot1);
	
	// Display the graph
	$graph->StrokeCSIM('proc/admin/totalPelanggaranDepartemen.php');
	break;
	
case 'graph-drill':
	$detachievID= $_REQUEST['detachievID'];
	$startPeriode = $_REQUEST['startPeriode'];
	$endPeriode = $_REQUEST['endPeriode'];
	include '../../view/admin/totalPelanggaranDepartemen/drill-total.php';
	break;
endswitch;